<?php

class Talktangle_CrossController extends Core_Controller_Action_Standard
{
  public function indexAction()
  {
     if( !$this->_helper->requireUser()->isValid() ) {
		return;
	 }
     $this->view->viewer = $viewer = Engine_Api::_()->user()->getViewer();   
     $id = $this->_getParam('id',0);
     if(empty($id)){
        $this->_forward('notfound');
     }
     
     $this->view->characters= $characters = 'I Love You Too';
     $coreApi =  Engine_Api::_()->talktangle();
     $this->view->matric = $coreApi->crossCharacters($characters,$id);
     $this->view->anphabetic = $coreApi->anphabetic;
     
  }
  
  public function uploadAction(){
    $result = array();
    $viewer = Engine_Api::_()->user()->getViewer();
    if(!$viewer || empty($viewer->user_id)){
        $result['error'] = true;
        $result['message'] = Zend_Registry::get('Zend_Translate')->_('Invalid request method');
        $data =  Zend_Json::encode($result);
        $js = <<<EOQ
          <script type="text/javascript">
            parent.$calback($data);
          </script>          
EOQ;
        exit($js);
    }
    $calback = $this->_getParam('callback','resultUploadIframe');
     //CHECK FORM VALIDAION
    if (!$this->getRequest()->isPost()) {
        $result['error'] = true;
        $result['message'] = Zend_Registry::get('Zend_Translate')->_('Invalid request method');
        $data =  Zend_Json::encode($result);
              $js = <<<EOQ
          <script type="text/javascript">
            parent.$calback($data);
          </script>          
EOQ;
        exit($js); 
      
    }
   

    //CHECK UPLOAD
    if (!isset($_FILES['image_upload']) || !is_uploaded_file($_FILES['image_upload']['tmp_name'])) {
        $result['error'] = true;
        $result['message'] = Zend_Registry::get('Zend_Translate')->_('Invalid Upload');
                $data =  Zend_Json::encode($result);
              $js = <<<EOQ
          <script type="text/javascript">
            parent.$calback($data);
          </script>          
EOQ;
      exit($js);   
    }
    $params = array(
        'parent_id' => 1,
        'parent_type' =>'ads'
    );
    $photoObj = Engine_Api::_()->getDbtable('crosstangles', 'talktangle')->uploadPhoto($params, $_FILES['image_upload']);
    $result['error'] = false;
    $result['id'] = $photoObj->file_id;
    $file = Engine_Api::_()->getApi('storage', 'storage')->get($photoObj->file_id, 'thumb.normal');
    $result['path'] = $file->map();
    $result['imageId'] = $file->file_id;
      
            $data =  Zend_Json::encode($result);
              $js = <<<EOQ
          <script type="text/javascript">
            parent.$calback($data);
          </script>          
EOQ;
 exit($js); 
  }
  public function createAction(){
    if( !$this->_helper->requireUser()->isValid() ) {
		return;
	}
    $this->view->viewer = $viewer = Engine_Api::_()->user()->getViewer();
    
    $core = Engine_Api::_()->talktangle();
    $userType = $core->getUserType($viewer);
    $form = new Talktangle_Form_Crosstangle_Create(array('type'=>$userType));
    $this->view->form = $form;
    // Check post
    if(!$this->getRequest()->isPost()){
        return;
    }
   
    if(!$form->isValid($this->getRequest()->getPost())){
        return;
    }
    
    $values = $form->getValues();
    $table = Engine_Api::_()->getDbTable('crosstangles','talktangle');
    $adsT = Engine_Api::_()->getDbTable('ads','talktangle');
    
    //save form values
    $db = $table->getAdapter();
    $db->beginTransaction();
    try
    {   
        $ads = null;
        if(!empty($values['imageId'])){
            $ads = $adsT->createRow();
            $ads->description = $values['ads_description'];
            $ads->owner_id =$viewer->getIdentity();
            $ads->photo_id = $values['imageId'];
            $ads->save();
        }
        
        $cross = $table->createRow();
        $cross->title = $values['title'];
        $cross->content = $values['content'];
        $cross->description = $values['description'];
        $cross->type = $userType;
        $cross->owner_id = $viewer->getIdentity();
        
        if($ads) {
            $cross->ad_id = $ads->getIdentity();
        }
        
        if($userType == 'company'){
            $cross->duration = time()+$values['duration'];
        }
        else {
            $durationUser = Engine_Api::_()->getApi('settings','core')->getSetting('talktangle.user.duration',60*10);
            $cross->duration = time()+$durationUser;
        }
        $cross->save();
        
        $promotionTable = Engine_Api::_()->getDbtable('promotions','talktangle');
        $promotion = $promotionTable->createRow()  ;
        $promotion->title = $values['promotion_title']   ;
        $promotion->description = $values['promotion_description'] ;
        $promotion->photo_id  = $values['promotion_id'];
        $promotion->owner_id = $viewer->user_id;
        $promotion->tangle_type = 'talktangle_crosstangle';
        $promotion->tangle_id = $cross->getIdentity();
        $promotion->save();
        
        $cross->promotion_id = $promotion->getIdentity();
        $cross->save();
        
        $db->commit();   
        if($userType =='company' && $ads){
            $actionT = Engine_Api::_()->getDbtable('actions','activity');
            $actionT->addActivity($viewer,$cross,'post_crosstangle');
            return $this->_helper->redirector->gotoRoute(array('controller'=>'cross','action'=>'index','id'=>$cross->getIdentity()), 'talktangle_extended', true);
        }
        else{
            $actionT = Engine_Api::_()->getDbtable('actions','activity');
            $actionT->addActivity($viewer,$cross,'post_crosstangle');
            return $this->_helper->redirector->gotoRoute(array('controller'=>'cross','action'=>'index','id'=>$cross->getIdentity()), 'talktangle_extended', true);
        }
                          
        
    }
    catch(Exception $e)
    {
        $db->rollBack();
        $this->view->error = $e->getMessage();
    } 
  }
}
